home *** CD-ROM | disk | FTP | other *** search
/ The AGA Experience 3 / AGA Experience Volume 3 (1997)(NFA - SAdENESS)[!].iso / software / utilities / graphics / rtgmaster / demos / mandel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-31  |  6.7 KB  |  278 lines

  1. /*
  2.  
  3.     RTG Library Usage Demo - MandelBrot Generator
  4.  
  5. */
  6. //* "Includes"
  7.  
  8.  
  9. /* Generic "Include Everything"-type file */
  10. #include <global.h>
  11. #include <math.h>
  12.  
  13.  
  14.  
  15. //*
  16. //* "Globals"
  17. struct RtgScreen *RtgScreen;
  18. struct ScreenReq *sr;
  19. struct RTGMasterBase *RTGMasterBase;
  20. struct Library *UtilityBase, *IntuitionBase;
  21. struct Library *GfxBase;
  22. struct TagItem rtag[] = {
  23.     smr_ChunkySupport,  -1,
  24.     smr_PlanarSupport,  -1,
  25.     smr_PrefsFileName,  (Tag)"MandelRTG.prefs",
  26.     TAG_DONE,           NULL
  27. };
  28.  
  29. struct TagItem gtag[] = {
  30.     grd_BytesPerRow,    0,
  31.     grd_Width,          0,
  32.     grd_Height,         0,
  33.     grd_Depth,          0,
  34.     grd_PixelLayout,    0,
  35.     grd_ColorSpace,     0,
  36.     grd_PlaneSize,      0,
  37.     grd_BusSystem,      0,
  38.     TAG_DONE,           0
  39. };
  40.  
  41. struct TagItem tacks[] = {
  42.     TAG_DONE,0
  43. };
  44.  
  45. BOOL Planar;
  46. UBYTE *cbuf=NULL;
  47. ULONG width;
  48. UBYTE *sadr;
  49. ULONG cmap[800];
  50. ULONG size;
  51. #define CBUF 76800
  52.  
  53.  
  54. typedef struct {
  55.     float x,y;
  56. } complex;
  57.  
  58.  
  59. const MAX=256;
  60.  
  61. /*
  62.  * This is done using an ugly direct hardware hit
  63.  * Should be an input handler or similar in future versions/real-life
  64.  * applications!
  65.  */
  66. extern int MouseButton(void);
  67.  
  68. //*
  69. //* "fail"
  70. void fail(void) {
  71.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  72.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  73.     if (UtilityBase) CloseLibrary(UtilityBase);
  74.     if (cbuf) FreeVec(cbuf);
  75.     exit(0L);
  76. }
  77. //*
  78. //* "Iterate"
  79. int iterate(complex zInit) {
  80.     complex z;
  81.     float a,b;
  82.     int cnt;
  83.  
  84.     z=zInit;
  85.     cnt=0;
  86.     while ((z.x*z.x+z.y*z.y<=4.0) && (cnt<MAX)) {
  87.     a=z.x*z.x-z.y*z.y;
  88.     b=2*z.x*z.y;
  89.     z.x=a; z.y=b;
  90.     z.x+=zInit.x;
  91.     z.y+=zInit.y;
  92.     cnt++;
  93.     }
  94.     return cnt;
  95. }
  96. //*
  97. //* "Mandelbrot"
  98. void Mandelbrot(float rMin, float rMax, float iMin, float iMax,
  99.     int cols, int rows) {
  100.     float rInc, iInc;
  101.     int x,y,count;
  102.     complex zInit;
  103.  
  104.     rInc=(rMax-rMin)/(float)cols;
  105.     iInc=(iMax-iMin)/(float)rows;
  106.     zInit.x=rMin;
  107.     for (x=0; x<cols; x++) {
  108.     zInit.y=iMin;
  109.     for (y=0; y<cols; y++) {
  110.         count=iterate(zInit);
  111.         WriteRtgPixel(RtgScreen,sadr,x,y,count);
  112.         zInit.y+=iInc;
  113.         if (MouseButton()) return;
  114.     }
  115.     zInit.x+=rInc;
  116.     }
  117. }
  118. //*
  119. //* "main"
  120. void main(int argc, char *argv[]) {
  121.    /*
  122.     * Since this is a demo, I don't check anything at all
  123.     * and simply assume that every open went ok... 8-)
  124.     */
  125.     int i,x;
  126.     struct TagItem *tag;
  127.     UBYTE rr, rg, rb;
  128.     ULONG width, height;
  129.     float iMin, iMax, rMin, rMax;
  130.     rMin=-2.25; rMax=0.75;
  131.     iMin=-1.25; iMax=1.25;
  132.  
  133.     if (argc>1) rMin=atof(argv[1]);
  134.     if (argc>2) rMax=atof(argv[2]);
  135.     if (argc>3) iMin=atof(argv[3]);
  136.     if (argc>4) iMax=atof(argv[4]);
  137.  
  138.  
  139.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  140.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  141.     IntuitionBase = OpenLibrary("intuition.library", 37L);
  142.     GfxBase = OpenLibrary("graphics.library", 37L);
  143.  
  144.     sr = RtgScreenModeReq(rtag);
  145.  
  146.     if (sr==NULL) fail();
  147.  
  148.     RtgScreen = OpenRtgScreen(sr, tacks);
  149.  
  150.     GetRtgScreenData(RtgScreen, gtag);
  151.  
  152.     tag=FindTagItem(grd_BytesPerRow, gtag);
  153.     size = tag->ti_Data;
  154.  
  155.     tag=FindTagItem(grd_Width, gtag);
  156.     width = tag->ti_Data;
  157.  
  158.     tag=FindTagItem(grd_PixelLayout, gtag);
  159.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  160.     printf("Screenmode not supported\n");
  161.     fail();
  162.     }
  163.  
  164.     printf("Screen pixel layout is ");
  165.     switch(tag->ti_Data) {
  166.     case grd_PLANAR:        printf("planar\n"); break;
  167.     case grd_PLANATI:       printf("interleaved planar\n"); break;
  168.     case grd_CHUNKY:        printf("8-Bit Z-Ordered (chunky)\n"); break;
  169.     case grd_HICOL15:       printf("15-Bit Chunky (2 Byte/pixel)\n"); break;
  170.     case grd_HICOL16:       printf("16-Bit Chunky (2 Byte/pixel)\n"); break;
  171.     case grd_TRUECOL24:     printf("24-Bit Chunky (3 Byte/pixel)\n"); break;
  172.     case grd_TRUECOL24P:    printf("24-Bit Chunky (3 Byteplanes/pixel)\n"); break;
  173.     case grd_TRUECOL32:     printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  174.     case grd_GRAFFITI:      printf("Graffiti 8 bit\n"); break;
  175.     case grd_TRUECOL32B:    printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  176.     default:                printf("unknown (%d)\n", tag->ti_Data); break;
  177.     }
  178.  
  179.     tag=FindTagItem(grd_ColorSpace, gtag);
  180.     if (tag->ti_Data) {
  181.     printf("Color space is ");
  182.     switch(tag->ti_Data) {
  183.         case grd_Palette:   printf("CLUT-Based\n"); break;
  184.         case grd_RGB:       printf("RGB (low-endian RGB)\n"); break;
  185.         case grd_BGR:       printf("BGR (high-endian RGB)\n"); break;
  186.         default:            printf("unknown (%d)\n", tag->ti_Data); break;
  187.     }
  188.     }
  189.  
  190.     tag=FindTagItem(grd_BusSystem, gtag);
  191.     if (tag->ti_Data) {
  192.     printf("Graphics card bus is ");
  193.     switch(tag->ti_Data) {
  194.         case grd_Z3:        printf("Zorro III\n"); break;
  195.         case grd_Z2:        printf("Zorro II\n"); break;
  196.         case grd_Custom:    printf("default custom chips\n"); break;
  197.         case grd_RGBPort:   printf("conneted to the RGB Port\n"); break;
  198.         case grd_GVP:       printf("GVP Special Bus (EGS110)\n"); break;
  199.         case grd_DDirect:   printf("DracCo® Direct\n"); break;
  200.         default:            printf("an unknown bus system\n"); break;
  201.     }
  202.     }
  203.  
  204.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  205.     else Planar = FALSE;
  206.  
  207.     tag=FindTagItem(grd_Width, gtag);
  208.     if (tag) width=tag->ti_Data;
  209.     tag=FindTagItem(grd_Height, gtag);
  210.     if (tag) height=tag->ti_Data;
  211.  
  212.     printf("Screen is %ld x %ld x %ld\n", width, height, gtag[3].ti_Data);
  213.     printf("It has %ld bytes per row\n", size);
  214.  
  215.  
  216.     if (Planar == TRUE) {
  217.     cbuf = AllocVec(CBUF, MEMF_CLEAR|MEMF_FAST);
  218.     if (cbuf==NULL) {
  219.         cbuf=AllocVec(CBUF, MEMF_CLEAR);
  220.         if (cbuf==NULL) {
  221.         printf("Out of memory *SIGH*\n");
  222.         fail();
  223.         }
  224.     }
  225.     }
  226.  
  227.     cmap[0] = 256 * 65536;
  228.     rr = 0;
  229.     rg = 0;
  230.     rb = 0;
  231.     x = 1;
  232.     for (i = 0; i < 64; i++) {
  233.     cmap[x++] = rr * 0x1111111;
  234.     cmap[x++] = rg * 0x1111111;
  235.     cmap[x++] = rb * 0x1111111;
  236.     rr += 3;
  237.     }
  238.     for (i = 0; i < 127; i++) {
  239.     cmap[x++] = rr * 0x1111111;
  240.     cmap[x++] = rg * 0x1111111;
  241.     cmap[x++] = rb * 0x1111111;
  242.     rg += 3;
  243.     }
  244.     for (i = 0; i < 60; i++) {
  245.     cmap[x++] = rr * 0x1111111;
  246.     cmap[x++] = rg * 0x1111111;
  247.     cmap[x++] = rb * 0x1111111;
  248.     rb += 3;
  249.     }
  250.     for (i = 0; i < 4; i++) {
  251.     cmap[x++]=0xFFFFFFFF;
  252.     cmap[x++]=0xFFFFFFFF;
  253.     cmap[x++]=0xFFFFFFFF;
  254.     }
  255.     cmap[x]=0;
  256.     LockRtgScreen(RtgScreen);
  257.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  258.     UnlockRtgScreen(RtgScreen);
  259.  
  260.     if (RtgScreen) {
  261.     LockRtgScreen(RtgScreen);
  262.     sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  263.  
  264.     //Mandelbrot(-1.08816, -0.93391, 0.36621, 0.25723, width, height);
  265.     Mandelbrot(rMin, rMax, iMin, iMax, width, height);
  266.     while (MouseButton()==0);
  267.     UnlockRtgScreen(RtgScreen);
  268.     CloseRtgScreen(RtgScreen);
  269.     }
  270.  
  271.     CloseLibrary(GfxBase);
  272.     CloseLibrary((struct Library *)RTGMasterBase);
  273.     CloseLibrary(UtilityBase);
  274.     CloseLibrary(IntuitionBase);
  275. }
  276. //*
  277.  
  278.